package cn.lzd.mr.PracticeGroup;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by lzd on 2018/8/3.
 */
public class PBean implements WritableComparable<PBean> {

    public PBean() {
    }

    public PBean(String name, String filename, String fullText) {
        this.name = name;
        this.filename = filename;
        this.fullText = fullText;
    }

    private String name;
    private String filename;
    private String fullText;

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(this.name);
        dataOutput.writeUTF(this.filename);
        dataOutput.writeUTF(this.fullText);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.name = dataInput.readUTF();
        this.filename = dataInput.readUTF();
        this.fullText = dataInput.readUTF();
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void setFullText(String fullText) {
        this.fullText = fullText;
    }

    public String getName() {
        return name;
    }

    public String getFilename() {
        return filename;
    }

    public String getFullText() {
        return fullText;
    }

    @Override
    public String toString() {
        return "PBean{" +
                "name='" + name + '\'' +
                ", filename='" + filename + '\'' +
                ", fullText='" + fullText + '\'' +
                '}';
    }

    @Override
    public int compareTo(PBean o) {
        System.out.println(Thread.currentThread().getName() + "===============map compareto===============");
        String key1 = null;
        String key2 = null;
        if("fl".equals(o.getFilename())){
            key1 = o.getName();
            key2 = this.getName();
        }else if("fl".equals(this.getFilename())){
            key1 = this.getName();
            key2 = o.getName();
        }else{
            key1 = this.getName();
            key2 = o.getName();
        }

        if (key1.indexOf(key2) != -1) {
            return 0;
        }
        return 1;
    }

    public static void main(String[] args) {
        PBean pBean1 = new PBean("啊","1","1");
        PBean pBean2 = new PBean("吧","1","1");
        PBean pBean3 = new PBean("从","1","1");
        List<PBean> list1 = Arrays.asList(pBean1, pBean2, pBean3);


        PBean pBean11 = new PBean("撒啊","1","1");
        PBean pBean22 = new PBean("撒吧","1","1");
        PBean pBean33 = new PBean("撒按时","1","1");
        List<PBean> list2 = Arrays.asList(pBean11, pBean22, pBean33);

        Collections.sort(list2);
        Collections.sort(list1);

        List<PBean> list3 = new ArrayList<>();
        list3.addAll(list1);
        list3.addAll(list2);


        Collections.sort(list3);

        list3.forEach(System.out::println);
//        list2.forEach(System.out::println);


    }
}
